<?php
class UsersController extends AppController {
	var $name = 'Users';
	var $authAllow = array('login', 'logout');
	var $scaffold;
	
	function admin_index(){
		$this->pageTitle = 'Users';
		$users = $this->User->findAll();
		$this->set(compact('users'));
	}
	
	function admin_add(){
		if(!empty($this->data)){
			if($this->User->save($this->data)){
				$this->Session->setFlash('Successfully saved.');
				$this->redirect(array('action' => 'index'));
			}
		}
		$this->pageTitle = 'Add User';
		$this->set('action', 'add');
		$this->render('admin_form');
	}
	
	function admin_edit(){
		if(!($this->{$this->modelClass}->id = intval(func_get_arg(0))) || !$this->{$this->modelClass}->exists()){
			$this->Session->setFlash('No such user.');
			$this->redirect(array('action' => 'index'));
		}
		if(!empty($this->data)){
			if($this->User->save($this->data)){
				$this->Session->setFlash('Successfully saved.');
				$this->redirect(array('action' => 'index'));
			}
		}
		else{
			$this->data = $this->{$this->modelClass}->read();
		}
		$this->pageTitle = 'Edit User';
		$this->set('action', 'edit');
		$this->render('admin_form');
	}
	
	
	function login(){
		$this->pageTitle = 'Login';
		if(!empty($this->Auth->data)) {
			if($this->Auth->login($this->Auth->data)) {
				$this->Session->setFlash("You've successfully logged in.");
				$this->redirect($this->Auth->redirect());
			}
		}
	}
	
	function logout(){
		$this->Session->setFlash('You have been logged out.');
		$this->redirect($this->Auth->logout());
	}
}
?>